#include "BlockQueue.hpp"
#include "Thread.hpp"
#include "Task.hpp"
#include <string>
#include <vector>
#include <unistd.h>
#include<ctime>

using namespace ThreadModule;
using blockqueue_t = BlockQueue<Task>;

void PrintHello()
{
    std::cout << "hello world" << std::endl;
}

void Productor(blockqueue_t &bq)
{
    while(true)
    {
        sleep(1);
        Task t=PrintHello;
        //放入任务
        bq.Enqueue(t);
    }
}

void Consumer(blockqueue_t &bq)
{
    while(true)
    {
        //从bq中取出任务
        Task t;
        bq.Pop(&t);

        //处理任务
        t();
    }
}
void StartComm(std::vector<Thread<blockqueue_t>> *threads, int num, blockqueue_t &bq,func_t<blockqueue_t> func)
{
    for(int i=0;i<num;i++)
    {
        std::string name="thread-"+std::to_string(i+1);
        threads->emplace_back(func,bq,name);
        threads->back().Start();
    }
}
void StartProductor(std::vector<Thread<blockqueue_t>> *threads, int num, blockqueue_t &bq)
{
   StartComm(threads,num,bq,Productor);
}

void StartConsumer(std::vector<Thread<blockqueue_t>> *threads, int num, blockqueue_t &bq)
{
   StartComm(threads,num,bq,Consumer);
}

void WaitAllThread(std::vector<Thread<blockqueue_t>> &threads)
{
    for(auto &thread:threads)
    {
        thread.Join();
    }
}
int main()
{
    // 需要执行的任务
    blockqueue_t *bq = new blockqueue_t(5);
    // 存储线程
    std::vector<Thread<blockqueue_t>> threads;

    StartProductor(&threads,3,*bq);
    StartConsumer(&threads,1,*bq);

    WaitAllThread(threads);

    return 0;
}